package com.wigem.approve.service;

import java.util.List;

import com.wigem.framework.common.pojo.PageResult;
import com.wigem.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.wigem.approve.entity.DicReportEntity;
import com.wigem.approve.mapper.DicReportMapper;
import com.wigem.approve.vo.DicReportVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 报告期字典Service业务层处理
 *
 * @author DH
 * @date 2024-05-27
 */
@Service
public class DicReportServiceImpl implements DicReportService {

    @Resource
    private DicReportMapper dicReportMapper;


    @Override
    public List<DicReportEntity> getList(DicReportVo dicReportvo) {

        return dicReportMapper.selectList(query(dicReportvo));
    }

    @Override
    public PageResult<DicReportEntity> getPageList(DicReportVo dicReportVo) {
        return dicReportMapper.selectPage(dicReportVo, query(dicReportVo));
    }


    @Override
    public DicReportEntity getEntity(String id) {
        return dicReportMapper.selectById(id);
    }


    @Override
    public    DicReportEntity getCurrentReportId(String stageId) {
        DicReportVo dicReportvo = new DicReportVo();
        if("1".equals(stageId)){
            dicReportvo.setStatus("0");
        }else if ("2".equals(stageId)){
            dicReportvo.setMidStatus("0");
        }else if ("3".equals(stageId)){
            dicReportvo.setPostStatus("0");
        }else{
            dicReportvo.setStatus("0");
        }
        if(!dicReportMapper.selectList(query(dicReportvo)).isEmpty()){
            return dicReportMapper.selectList(query(dicReportvo)).get(0);
        }else{
            return new DicReportEntity() ;
        }
    }

    @Override
    public void add(DicReportVo dicReportVo) {
        DicReportEntity dicReportEntity = new DicReportEntity();
        BeanUtils.copyProperties(dicReportVo, dicReportEntity);
        dicReportEntity.create();
        dicReportMapper.insert(dicReportEntity);
    }


    @Override
    public void update(DicReportVo dicReportVo) {
        DicReportEntity dicReportEntity = new DicReportEntity();
        BeanUtils.copyProperties(dicReportVo, dicReportEntity);
        dicReportMapper.updateById(dicReportEntity);
    }

    @Override
    public void save(DicReportVo dicReportVo) {

        DicReportEntity dicReportEntity = new DicReportEntity();
        BeanUtils.copyProperties(dicReportVo, dicReportEntity);
        if (StringUtils.isEmpty(dicReportVo.getId())) {
            dicReportEntity.create();
            dicReportMapper.insert(dicReportEntity);
        } else {
            dicReportMapper.updateById(dicReportEntity);
        }

    }


    @Override
    public int delete(String id) {
        return dicReportMapper.deleteById(id);
    }


    @Override
    public int remove(String id) {
        return dicReportMapper.remove(id);
    }


    public LambdaQueryWrapperX<DicReportEntity> query(DicReportVo dicReportVo) {

        LambdaQueryWrapperX<DicReportEntity> dicReportEntityLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        dicReportEntityLambdaQueryWrapperX = dicReportEntityLambdaQueryWrapperX
                .eqIfPresent(DicReportEntity::getId, dicReportVo.getId())
                .likeIfPresent(DicReportEntity::getReportName, dicReportVo.getReportName())
                .eqIfPresent(DicReportEntity::getBeginTime, dicReportVo.getBeginTime())
                .eqIfPresent(DicReportEntity::getEndTime, dicReportVo.getEndTime())
                .eqIfPresent(DicReportEntity::getStatus, dicReportVo.getStatus())
                .eqIfPresent(DicReportEntity::getReportType, dicReportVo.getReportType())
                .eqIfPresent(DicReportEntity::getReportYear, dicReportVo.getReportYear())
                .eqIfPresent(DicReportEntity::getMidStatus, dicReportVo.getMidStatus())
                .eqIfPresent(DicReportEntity::getPostStatus, dicReportVo.getPostStatus())
        ;
        return dicReportEntityLambdaQueryWrapperX;
    }


}
